The microcontrollers in the STM32 family are characterized by high computing power and a large number of I/O pins. Special development boards such as the Nucleo F401RE, which can be purchased for around 24 euros, are available to get started with programming. If you take a look at a pinout of the board (Fig. 1 and 2), you will immediately notice that the Nucleo F401R is pin-compatible with the Arduino. This means that all shields that are available for the Arduino can also be used for the Nucleo. Of course, the Nucleo board has considerably more interfaces than the standard Arduino. A technical data overview can be seen in Table 1.
Housing LQFP64 |
ARM 32-bit Cortex-M4 CPU with FPU |
84 MHz CPU |
Operating voltage 1.7 V to 3.6 V |
512 KB Flash |
96 KB SRAM |
50 x GPIOs |
16 x 12 bit AD converter |
Real-time clock |
Extended timer functions |
2 x watchdog timer |
4 x USART/UART |
3 x I2C |
3 x SPI |
SDIO |
USB 2.0 |
The development environment: STM32CubeIDE
Great hardware is only one success factor during development, a well-tuned IDE for hardware is a much more important point. For the microcontrollers of the STM32 family, there is a whole range of development environments – from very expensive to free-ware, everything is available. In this article, we use the STM32CubeIDE. It can be downloaded for free and is available for Linux, Mac, and Windows. However, you have to give your email address to start the download. But this is a small price to pay, considering how powerful the IDE is. Linux installation runs entirely on the command line and can be done without any problems. Under Linux, however, the installation must be run with root permission.
After installation, the IDE can be started comfortably from the application overview. If you do not see the IDE right away, just search for STM. At first start, the STM32CubeIDE asks where the workspace should be created. After that, the IDE presents itself as shown in Figure 3.
Now, we connect the Nucleo board via USB to our PC so that we can start with the first small test. To do this, click Start new STM32 project in the STM32Cube IDE. A window opens where we can select the STM32 target system. The family of STM32 microcontrollers is large, with 1796 different boards at the time of writing. To quickly find the Nucleo board, click on the Board Selector tab, select Nucleo-64 for the type, and scroll down the list until you see the NUCLEO-F401RE board. With one click, we select the board as the target system for our project. In the next step, we are asked to name the project and decide upon Blink. The options can be left at the preselected values. If we click the Finish button, the project will be created. Then, answer the questions about initializing the periphery and the perspective with yes. It takes a moment for all necessary project files to download. This is a good time to make a cup of coffee. When all components for the project are installed, the Pinout & Configuration screen opens as shown in Figure 4.
To understand what we are seeing on the screen right now, we have to backtrack a bit. The microcontrollers in the STM32 family are extremely versatile and configurable within wide limits. This means we can freely select the function for each pin of the controller. We are not only talking about input and output, but also about different interfaces (such as I2C, SPI and UART) and special functions that can be assigned to the individual pins. Explaining all existing possibilities would take too much space here.
You can take a look at the individual categories on the left to get an idea of what can be set. For us, the only important thing at the moment is that the green LED of the Nucleo board is connected to pin PA5. For now, the hardware configuration is complete. We can now start writing the actual program for the STM32.
To do this, we open the file main.c. You can easily find the file by clicking on the tree structure in the project explorer (Fig. 5). Already with the first look into the source code you will notice that there are certain areas for user code. Use only the designated areas for user code to write your own programs. If you create code outside these areas, it will be overwritten by the IDE without mercy. Also, don’t change the existing comments, that will cause a lot of trouble. If you want to change something in the hardware configuration later, don’t do it in the code, but always via the configuration functions shown at the beginning (Fig. 4). You must always keep in mind that large parts of the source code are generated automatically. Of course, this only works if you don’t disturb the code generator.
Now we come to our first small program. This program makes the LED on the Nucleo board blink every second. Look for the area in the main.c file that is intended for the user code. Here, insert the lines as shown in Listing 1. You can use the comments as a good guide.
To load the code into the controller and run it, click on the Run icon (white arrow pointing right in the green circle). The first time we do this, we are asked a few questions, all of which can be answered with yes. After a short moment, the program is in the controller and will be executed.
Listing 1: Simple blink program
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_5);
HAL_Delay (1000);
/* USER CODE END WHILE */
When uploading the software to the controller, it’s possible that the controller firmware is older than the IDE expects. Then, after some errors, you will end up in the dialog from Figure 6. First, click Open in update mode and then Upgrade to bring the firmware in your controller up to date. After the upgrade, you can try again to load the program into the controller.
We have shown in fast-forward how to create a program for the STM32 Nucleo F401RE board and load it into the controller. We have only gone into absolutely necessary points, and did not show many useful functions. If you want to learn more about STM32, you can start with the STM32 Wiki. Here many basic functions of the microcontroller are described in an easy-to-understand way. In the following sections, we will look at how to make the STM32 controller Internet-capable and how to build a simple web server on the controller.
A GLIMPSE INTO IoT
SMART DATA SOLUTIONS
WIZ5500 Ethernet Controller
In order to connect the Nucleo board to a network, we first need a suitable interface. The WIZ5500 is a hardware Ethernet controller that supports the usual protocols such as TCP, UDP, ICMP, IPv4, ARP, IGMP, and PPPoE. Its operating voltage is 3.3 volts. The Ethernet controller operates at both 10 Mbit and 100 Mbit. As a hobbyist-friendly module, the WIZ5500 costs about 9 euros. The data transfer between the network module and the Nucleo board is done via the SPI interface. This is often used for the communication of microcontrollers with their peripherals. In principle, the data is transmitted clock-synchronously and serially. Table 2 shows how the Nucleo Board must be connected to the network module. It should be noted here that we are using the SPI2 interface of the Nucleo board. Figure 7 shows how to make the connections. To make sure we don’t forget later, we should connect the LAN directly as well. It wouldn’t be the first time that we spent hours looking for a fault—and in the end, it was only a forgotten plug connection.
5V | 5V |
PC2 | MISO |
PC3 | MOSI |
PB10 | SCLK |
PC4 | SCS |
GND | GND |